package com.wm.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

import com.wm.bean.Area;
import com.wm.bean.City;
import com.wm.bean.Scriptorium;
import com.wm.bean.UserScriptorium;
import com.wm.comm.Constant;
import com.wm.service.AreaService;
import com.wm.service.CityService;
import com.wm.service.ScriptoriumService;
import com.wm.service.UserScriptoriumService;
import com.wm.web.servlet.UserSession;
import com.wm.web.ui.OptionsLong;

public class UserScriptoriumAction extends BaseAction implements SessionAware{

	private static final Log logger = LogFactory.getLog(UserScriptoriumAction.class);
	
	private CityService cityService;
	private AreaService areaService;
	private ScriptoriumService scriptoriumService;
	private UserScriptoriumService userScriptoriumService;
	
	public CityService getCityService() {
		return cityService;
	}

	public void setCityService(CityService cityService) {
		this.cityService = cityService;
	}
	
	public AreaService getAreaService() {
		return areaService;
	}

	public void setAreaService(AreaService areaService) {
		this.areaService = areaService;
	}
	
	public ScriptoriumService getScriptoriumService() {
		return scriptoriumService;
	}

	public void setScriptoriumService(ScriptoriumService scriptoriumService) {
		this.scriptoriumService = scriptoriumService;
	}

	public UserScriptoriumService getUserScriptoriumService() {
		return userScriptoriumService;
	}

	public void setUserScriptoriumService(
			UserScriptoriumService userScriptoriumService) {
		this.userScriptoriumService = userScriptoriumService;
	}
	
	private Map session;

	public void setSession(Map session){
    	this.session = session;
    }
	
	public Map getSession() {
		return session;
	}
	
	private List cityList = new ArrayList();
	private List areaList = new ArrayList();
	
	public List getCityList() {
		return cityList;
	}

	public void setCityList(List cityList) {
		this.cityList = cityList;
	}

	public List getAreaList() {
		return areaList;
	}

	public void setAreaList(List areaList) {
		this.areaList = areaList;
	}
	
	private void setCityListValues(){
		cityList.add(new OptionsLong(-1, "请选择"));
		List l = this.getCityService().findCitysByIsShow(1, "id", 0);
		for(int i=0;i<l.size();i++){
			City c = (City) l.get(i);
			cityList.add(new OptionsLong(c.getId(), c.getCityName()));
		}
	}
	private void setAreaListValues(){
		areaList.add(new OptionsLong(-1, "请选择"));
	}
	
	private void listInitValues(){
		setCityListValues();
		setAreaListValues();
		this.setAreas(this.getAreaService().findAreasByIsShow(1, "id", 0));
	}
	
	private int scrType;				//写字楼类型 1:写字间 2:住宅 3:商铺
	private long cityId = -1;			//城市ID
	private long areaId = -1;			//地区ID
	private List areas;
	private String scrName;
	public List slist;
	public List uslist;
	
	private String jsonString;
	
	public String getJsonString() {
		return jsonString;
	}

	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}
	
	public String getScrName() {
		return scrName;
	}

	public void setScrName(String scrName) {
		this.scrName = scrName;
	}

	public int getScrType() {
		return scrType;
	}

	public void setScrType(int scrType) {
		this.scrType = scrType;
	}

	public long getCityId() {
		return cityId;
	}

	public void setCityId(long cityId) {
		this.cityId = cityId;
	}

	public long getAreaId() {
		return areaId;
	}

	public void setAreaId(long areaId) {
		this.areaId = areaId;
	}
	
	public List getAreas() {
		return areas;
	}

	public void setAreas(List areas) {
		this.areas = areas;
	}

	public List getSlist() {
		return slist;
	}

	public void setSlist(List slist) {
		this.slist = slist;
	}

	public List getUslist() {
		return uslist;
	}

	public void setUslist(List uslist) {
		this.uslist = uslist;
	}

	public String execute() {
		try {
			if(this.getSession().get(Constant.USER_SESSION_KEY)==null){
				return "login";
			}
			return this.executeMethod(this.getAction());
		} catch (Exception ex) {
			logger.error(ex);
			return ERROR;
		}
	}
	
	public String index() {
		listInitValues();
		return "index";
	}
	
	public String save(){
		UserSession us = (UserSession)this.getSession().get(Constant.USER_SESSION_KEY);
		if(this.getScrName()!=null && !this.getScrName().trim().equals("")){
			//写字间
			if(scrType==1){
				this.setSlist(this.getUserScriptoriumService().findUserScriptoriumByscrName(scrName));
				if(slist!=null){
					
				}else{
					this.setUslist(this.getScriptoriumService().findScriptoriumByScrName(scrName));
				}
				UserScriptorium u = new UserScriptorium();
				u.setUserId(us.getUid());
				u.setScrName(scrName);
				u.setCityId(cityId);
				u.setAreaId(areaId);
				u.setAddTime(new Date());
				this.getUserScriptoriumService().saveUserScriptorium(u);
			}
			//住宅
			else if(scrType==2){
				
			}
			//商铺
			else if(scrType==3){
				
			}
			
		}
		this.setAction("index");
		return "indexAction";
	}
	
	/**
	 * 搜索
	 * @return
	 */
	public String scriptorium(){
		if(this.getAreaId()>0){
			if(this.getScrType()==0){
				this.setScrType(1);
			}
			
			this.setSlist(this.getScriptoriumService().findScriptoriumsByScrInitialsScrAnglicScrPhoneticScrTypeAreaIdIsShow(null, null, null, this.getScrType(), this.getAreaId(), 1, "orderNum", 1));
			if(this.getSlist()!=null && this.getSlist().size()>0){
				String az = "";
				String rs = "";
				Boolean bl = false;
				for(int i=0;i<this.getSlist().size();i++){
					Scriptorium s = (Scriptorium)this.getSlist().get(i);
					String[] al = az.split(",");
					if(al.length>0){
						bl=true;
						if(al[0].equals("")){
							az="\""+s.getScrInitials()+"\"";
						}else{
							for(int j=0;j<al.length;j++){
								if(al[j].equals("\""+s.getScrInitials()+"\"")){
									bl=false;
								}
							}
							if(bl){
								az=az+",\""+s.getScrInitials()+"\"";
							}
						}
						
					}
					if(rs.equals("")){
						rs = "{\"id\":"+s.getId()+",\"sn\":\""+s.getScrInitials()+"\",\"name\":\""+s.getScrName()+"\"}";
					}else{
						rs = rs+","+"{\"id\":"+s.getId()+",\"sn\":\""+s.getScrInitials()+"\",\"name\":\""+s.getScrName()+"\"}";
					}
				}
				//this.setJsonString("{\"az_data\":{\"id\":\"123\",\"name\":\"JSONServlet\",\"say\":\"Hello , i am a servlet !\",\"password\":\"JSON\"},\"success\":true}");  
				this.setJsonString("{\"az_data\":["+az+"],\"reatan_data\":["+rs+"]}");
			}
		}
		return "jsonstring";
	}

}
